/* $Id: chatlist.cc jjs $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 2 or any
   later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/


#define DEBUG_ROUTINE "chatlist"

#include "chatlist.h"
#include "moros.h"
#include "../central/game.h"
#include "../general/log.h"


chatlist::chatlist(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml> refGlade)
: Gtk::Window(cobject), m_refGlade(refGlade)
{
	chatlist::show();
	m_refGlade->get_widget("chatlist_close", chatlist_close);
	m_refGlade->get_widget("chatlist_new", chatlist_new);
	m_refGlade->get_widget("chatlist_join", chatlist_join);
	m_refGlade->get_widget("chatlist_view", chatlist_view);

	list_store = Gtk::ListStore::create(cols);
	chatlist_view->set_model(list_store);
	chatlist_view->set_search_column(cols.name.index());

	int colnr;
	Gtk::TreeViewColumn* col;
	Gtk::CellRendererText* renderer;

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = chatlist_view->append_column("Name", *renderer);
	col = chatlist_view->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.name);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = chatlist_view->append_column("Users", *renderer);
	col = chatlist_view->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.members);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = chatlist_view->append_column("Type", *renderer);
	col = chatlist_view->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.type);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = chatlist_view->append_column("Status", *renderer);
	col = chatlist_view->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.status);

	renderer = Gtk::manage( new Gtk::CellRendererText() );
	colnr = chatlist_view->append_column("Description", *renderer);
	col = chatlist_view->get_column(colnr-1);
	col->add_attribute(renderer->property_text(), cols.descr);

    chatlist_view->signal_row_activated().connect(sigc::mem_fun(*this, &chatlist::clicked_list));
	chatlist_close->signal_clicked().connect(sigc::mem_fun(*this, &chatlist::clicked_close));
	chatlist_new->signal_clicked().connect(sigc::mem_fun(*this, &chatlist::clicked_new));
	chatlist_join->signal_clicked().connect(sigc::mem_fun(*this, &chatlist::clicked_join));
}

chatlist::~chatlist() {}

void chatlist::clear() {
	list_store->clear();
}

void chatlist::record(std::string name, int members, std::string type, std::string status,
    std::string descr) {
	Gtk::TreeRow row = *(list_store->append());
	row[cols.name]=name;
	row[cols.members]=members;
	row[cols.type]=type;
	row[cols.status]=status;
	row[cols.descr]=descr;
}

void chatlist::clicked_close()
{
	chatlist::hide();
}

void chatlist::clicked_new() {
    dbg_calls("New game");
}

void chatlist::clicked_list(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column) {
	Glib::RefPtr<Gtk::TreeSelection> sel=chatlist_view->get_selection();
    Gtk::TreeModel::iterator i=sel->get_selected();
    Glib::ustring channel=((*i)[cols.name]);
    dbg_calls("Clicked channel %s", channel.c_str());
}

void chatlist::clicked_join() {
}
